package com.supermap;

import com.supermap.fabricsdk.FabricsdkUtils;
import com.supermap.fabricsdk.SmChain;
import com.supermap.fabricsdk.SmTestThreadRead;
import com.supermap.fabricsdk.SmTestThreadWrite;

import java.io.File;
import java.util.Map;

/**
 * @author liqs
 * @version 1.0
 * @date 2020/8/6 17:15
 * 目标在 Linux 下运行跑
 */
public class Main {

    //    java -classpath useFabricSdkJava-1.0-SNAPSHOT.jar com.supermap.Main  blockchain-network-config.yaml 40 12000 ""
    public static void main(String[] args) {

        // 网络配置文件
        File networkFile = new File(args[0]);
        // 线程数
        int threadCount = Integer.valueOf(args[1]);
        // 总数
        int perThreadCount = Integer.valueOf(args[2]);
        // 参数（有就代表是读即 Key 值，无就代表是写）
        String readKey = args[3];

//        File networkFile = new File("D:\\blockchain-network-config.yaml");
//        int threadCount = 3;
//        int perThreadCount = 2000;
//        String readKey = "";

        Map<String, Object> networkFileMap = FabricsdkUtils.getNetworkFileMap(networkFile);
        String channelName = FabricsdkUtils.getChannelName(networkFileMap);
        String chaincodeName = FabricsdkUtils.getChainCodeName(networkFileMap);
        SmChain smChain = SmChain.getChain(channelName, networkFile);

        if( readKey.length() == 0 ) {
            long starttime = System.currentTimeMillis();
            Thread[] threads = new Thread[threadCount];
            for (int i = 0; i < threadCount; i++) {
                threads[i] = new SmTestThreadWrite(smChain, chaincodeName, perThreadCount, i);
                threads[i].start();
            }
            // 保证线程执行完
            for (int i = 0; i < threads.length; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            long endtime = System.currentTimeMillis();
            System.out.println("总耗时 : " + (endtime - starttime) + "ms");
            System.out.println("数据量 : " + perThreadCount * threadCount);
            System.out.println("TPS : " + perThreadCount * threadCount / ((endtime - starttime) / 1000) + "");
        } else {
            long starttime = System.currentTimeMillis();
            Thread[] threads = new Thread[threadCount];
            int start = 0;
            for (int i = 0; i < threadCount; i++){
                threads[i] = new SmTestThreadRead(smChain, chaincodeName, perThreadCount, i, start, readKey);
                threads[i].start();
            }
            // 保证线程执行完
            for (int i = 0; i < threads.length; i++){
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
            long endtime = System.currentTimeMillis();
            System.out.println("读数据总耗时 = " + (endtime - starttime) + "ms");
            System.out.println("TPS = " + perThreadCount * threadCount / ((endtime - starttime)/1000)  + "");
        }

    }



}
